package cn.iocoder.yudao.module.els.service.codeGeneratorDetail;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.els.controller.admin.codeGeneratorDetail.vo.CodeGeneratorDetailPageReqVO;
import cn.iocoder.yudao.module.els.controller.admin.codeGeneratorDetail.vo.CodeGeneratorDetailSaveReqVO;
import cn.iocoder.yudao.module.els.dal.dataobject.codeGeneratorDetail.CodeGeneratorDetailDO;
import cn.iocoder.yudao.module.els.dal.mysql.codeGeneratorDetail.CodeGeneratorDetailMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.els.enums.ErrorCodeConstants.CODE_GENERATOR_DETAIL_NOT_EXISTS;

/**
 * 编码生成器明细 Service 实现类
 *
 * @author 吕小文
 */
@Service
@Validated
public class CodeGeneratorDetailServiceImpl implements CodeGeneratorDetailService {

    @Resource
    private CodeGeneratorDetailMapper codeGeneratorDetailMapper;

    @Override
    public Long createCodeGeneratorDetail(CodeGeneratorDetailSaveReqVO createReqVO) {
        // 插入
        CodeGeneratorDetailDO codeGeneratorDetail = BeanUtils.toBean(createReqVO, CodeGeneratorDetailDO.class);
        codeGeneratorDetailMapper.insert(codeGeneratorDetail);
        // 返回
        return codeGeneratorDetail.getId();
    }

    @Override
    public void updateCodeGeneratorDetail(CodeGeneratorDetailSaveReqVO updateReqVO) {
        // 校验存在
        validateCodeGeneratorDetailExists(updateReqVO.getId());
        // 更新
        CodeGeneratorDetailDO updateObj = BeanUtils.toBean(updateReqVO, CodeGeneratorDetailDO.class);
        codeGeneratorDetailMapper.updateById(updateObj);
    }

    @Override
    public void deleteCodeGeneratorDetail(Long id) {
        // 校验存在
        validateCodeGeneratorDetailExists(id);
        // 删除
        codeGeneratorDetailMapper.deleteById(id);
    }

    private void validateCodeGeneratorDetailExists(Long id) {
        if (codeGeneratorDetailMapper.selectById(id) == null) {
            throw exception(CODE_GENERATOR_DETAIL_NOT_EXISTS);
        }
    }

    @Override
    public CodeGeneratorDetailDO getCodeGeneratorDetail(Long id) {
        return codeGeneratorDetailMapper.selectById(id);
    }

    @Override
    public PageResult<CodeGeneratorDetailDO> getCodeGeneratorDetailPage(CodeGeneratorDetailPageReqVO pageReqVO) {
        return codeGeneratorDetailMapper.selectPage(pageReqVO);
    }

}